package com.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.mybatis.Page;
import com.common.utils.DateUtils;
import com.common.utils.base.UuidUtil;
import com.entity.AttachmentRelationship;
import com.entity.NewsInformation;
import com.repository.AttaRelationshipDao;
import com.repository.NewsInformationDao;
import com.service.base.BaseServiceImp;

@Service("NewsInformationService")
public class NewsInformationServiceImp extends BaseServiceImp<NewsInformation> implements NewsInformationService{

	//附件关系
	@Autowired
	private AttaRelationshipDao fjgxDao;
	//新闻详情
	@Autowired
	private NewsInformationDao NewsInforDao;
	
	
	@Transactional
	@Override
	public void insterFj(NewsInformation o, String AttachmentId) {
		String newsInforId=UuidUtil.get32UUID();
		o.setNewsInformationId(newsInforId);
		o.setReleaseTime(DateUtils.getDate("yyyy/MM/dd HH:mm:ss"));
		String fujianArry[];
		if(AttachmentId.contains(",")){
			fujianArry=AttachmentId.split(",");
		}else{
			fujianArry=new String[]{AttachmentId};
		}
		if(AttachmentId!=null){
			for(int i=0;i<fujianArry.length;i++){
				AttachmentRelationship fujiangx=new AttachmentRelationship();
				fujiangx.setAttachmentRelationshipId(UuidUtil.get32UUID());
				fujiangx.setAssociatedId(newsInforId);
				fujiangx.setAttachmentId(fujianArry[i]);
				fujiangx.setAssociatedName("newsInformation");
				fjgxDao.insert(fujiangx);
			}
		}
		NewsInforDao.insert(o);
	}

	@Transactional//事务回滚
	@Override
	public void updateAll(NewsInformation o, String AttachmentId) {
		System.out.println("NewsInformation"+o);
		System.out.println("AttachmentId"+AttachmentId);
		String fujianArry[];
		if(AttachmentId.contains(",")){
			fujianArry=AttachmentId.split(",");
		}else{
			fujianArry=new String[]{AttachmentId};
		}
		System.out.println("*************************Update:AttachmentId:"+AttachmentId);
		System.out.println("*************************Update:fujianArry"+fujianArry[0]);
		if(AttachmentId!=null){
			for(int i=0;i<fujianArry.length;i++){
				AttachmentRelationship fujiangx=new AttachmentRelationship();
				fujiangx.setAttachmentRelationshipId(UuidUtil.get32UUID());
				fujiangx.setAssociatedId(o.getNewsInformationId());
				fujiangx.setAttachmentId(fujianArry[i]);
				fujiangx.setAssociatedName("newsInformation");
				fjgxDao.insert(fujiangx);
			}
		}
		NewsInforDao.update(o);
	}
	
	
	@Override
	public Page<NewsInformation> findByPageByNewid(NewsInformation o,
			Page<NewsInformation> page,String n) {
		page.setResults(NewsInforDao.findByPageByNewid(o, page,n));
		return page;
	}

}
